package com.freexiaoyu.freevideoplay;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import com.freexiaoyu.freevideoplay.FreeH.FreeHMainActivity;
import com.freexiaoyu.freevideoplay.R;
import com.freexiaoyu.freevideoplay.bean.MessageEvent;
import com.freexiaoyu.freevideoplay.databinding.ActivityMainBinding;
import com.freexiaoyu.freevideoplay.ui.base.BaseActivity;
import com.freexiaoyu.freevideoplay.utils.Camera;
import com.freexiaoyu.freevideoplay.utils.DES3Utils;
import com.freexiaoyu.freevideoplay.utils.NetUtil;
import com.freexiaoyu.freevideoplay.utils.PermissionsChecker;
import com.freexiaoyu.freevideoplay.utils.ToastUtil;
import com.freexiaoyu.freevideoplay.widget.LoadingDialog;

import java.io.File;


public class MainActivity extends BaseActivity<ActivityMainBinding> {
    private final static int RESULT_CODE_PERMISSIONS = 0x001;
    private final static int RESULT_CODE_CAMERA = 0x002;
    private PermissionsChecker mPermissionsChecker; // 权限检测器
    //文件写入读取权限 手机状态
    String[] perms = {
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.MANAGE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.BLUETOOTH,
            Manifest.permission.BLUETOOTH_ADMIN,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.BLUETOOTH_SCAN,
            Manifest.permission.BLUETOOTH_ADVERTISE,
            Manifest.permission.BLUETOOTH_CONNECT};

    static LoadingDialog loadingDialog = null;
    static LoadingDialog.Builder loadBuilder;


    @Override
    protected int provideContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void init() {
        mPermissionsChecker = new PermissionsChecker(this);
    }

    @Override
    protected void initView() {
        // 缺少权限时, 进入权限配置页面
        if (mPermissionsChecker.lacksPermissions(perms)) {
            ActivityCompat.requestPermissions(MainActivity.this, perms, RESULT_CODE_PERMISSIONS);
        }
    }

    @Override
    protected void initData() {
        LoadingDialog.Builder loadBuilder = new LoadingDialog.Builder(this)
                .setCancelable(true)
                .setShowMessage(true)
                .setMessage("加载中,请稍后...")
                .setCancelOutside(false);
        loadingDialog = loadBuilder.create();

    }

    @Override
    protected void initListener() {
        mBinding.btnSubmit.setOnClickListener(v -> {
            serverSocketExample = new ServerSocketExample();
            serverSocketExample.startServer();
        });
        mBinding.btnSubmit2.setOnClickListener(v -> {
            mIntent = new Intent(mContext, VideoStreamPlayerActivity.class);
            startActivity(mIntent);
        });
        mBinding.btnSubmit3.setOnClickListener(v -> {
            mIntent = new Intent(mContext, VideoFFmpegActivity.class);
            startActivity(mIntent);
        });
        mBinding.btnSubmit4.setOnClickListener(v -> {
            mIntent = new Intent(mContext, ExoPlayerActivity.class);
            startActivity(mIntent);
        });

        mBinding.btnSubmit5.setOnClickListener(v -> {
            //mIntent = new Intent(mContext, VideoFFmpegTwoActivity.class);
            mIntent = new Intent(mContext, ExoPlayerTwoActivity.class);
            startActivity(mIntent);
        });

        mBinding.btnSubmit6.setOnClickListener(v -> {
            //mIntent = new Intent(mContext, FreeHMainActivity.class);
            mIntent = new Intent(mContext, VideoFFmpegTwoActivity.class);
            startActivity(mIntent);
        });

        mBinding.btnSubmit7.setOnClickListener(v -> {
            mIntent = new Intent(mContext, FFmepgKitTestActivity.class);
            startActivity(mIntent);
        });

        mBinding.btnSubmit8.setOnClickListener(v -> {
            mIntent = new Intent(mContext, com.freexiaoyu.freevideoplay.FreeH2.MainActivity.class);
            startActivity(mIntent);
        });

        mBinding.btnSubmit9.setOnClickListener(v -> {
            mIntent = new Intent(mContext, VideoFFmpegThreeActivity.class);
            startActivity(mIntent);
        });

        mBinding.btnSubmit10.setOnClickListener(v -> {
            mIntent = new Intent(mContext, VideoFourActivity.class);
            startActivity(mIntent);
        });

        mBinding.btnSubmit11.setOnClickListener(v -> {
            mIntent = new Intent(mContext, VideoActivity2.class);
            startActivity(mIntent);
        });
        mBinding.btnSubmit12.setOnClickListener(v -> {
            mIntent = new Intent(mContext, VideoUDPAcivity.class);
            startActivity(mIntent);
        });

        mBinding.btnPing.setOnClickListener(v -> {
            String ipStr = mBinding.etIp.getText().toString();
            if (TextUtils.isEmpty(ipStr)) {
                ToastUtil.show(mContext, "请输入IP地址");
                return;
            }
            loadingDialog.show();
            new Thread(() -> {
                //int result=NetUtil.ping(ipStr);
                boolean result = NetUtil.isNetworkHasTraffic(ipStr);
                Message msg = mHandler.obtainMessage();
                msg.what = 10001;
                msg.obj = result;
                mHandler.sendMessage(msg);
            }).start();
        });
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            loadingDialog.dismiss();
            if (msg.what == 10001) {
                if (msg.obj == null) {
                    return;
                }
                if ((boolean) msg.obj) {
                    ToastUtil.show(mContext, "连接成功");
                    mBinding.tvInfo.setText("连接成功");
                } else {
                    ToastUtil.show(mContext, "连接失败");
                    mBinding.tvInfo.setText("连接失败");
                }
            }
        }
    };

    ServerSocketExample serverSocketExample = null;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (serverSocketExample != null) {
            serverSocketExample.stopServer();
        }
    }

    @Override
    protected void messageHandler(MessageEvent event) {

    }


    @Override
    public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(permsRequestCode, permissions, grantResults);
        switch (permsRequestCode) {
            case RESULT_CODE_PERMISSIONS:
                boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                if (!cameraAccepted) {
                    ToastUtil.show(mContext, "缺少主要权限, 无法运行");
                    finish();
                }
                break;
        }
    }


}
